//
//  themeTableViewCell.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "themeTableViewCell.h"
#import "HTTPTool.h"
#import "AFNetworking.h"
#import "themeModel.h"
#import "UIButton+WebCache.h"
@implementation themeTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.themeScrollView =[[UIScrollView alloc]initWithFrame:CGRectZero];
    self.themeScrollView.backgroundColor=[UIColor whiteColor];
    self.themeScrollView.contentSize=CGSizeMake(115*9, 0);
    
    [self.contentView addSubview:self.themeScrollView];
    
        self.buttonarr =[NSMutableArray array];
    
    self.themeScrollView.pagingEnabled=NO;
    //是否边界 回弹
    self.themeScrollView.bounces=NO;
    //是否有水平滚动条
    self.themeScrollView.showsHorizontalScrollIndicator=NO;
    
    //    [self createmyimage];
    
    //    [self.contentView addSubview:self.myImageView];
    [self.myImageView release];

    [self.themeScrollView release];
    [self createData];
    
    
    return  self;
    
    
}


- (void)createData
{
    self.themeArr=[NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://www.hunliji.com/v1/api/app/newsubjects.json?page=1&per_page=15" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSMutableArray  *arr = [NSMutableArray array];
        arr = dic[@"subjects"];
        
        for (NSDictionary *tempdic in arr) {
            themeModel *theme = [[themeModel alloc]init];
            [theme setValuesForKeysWithDictionary:tempdic];
            [self.themeArr addObject:theme];
            [self.themeScrollView reloadInputViews];
        
        }
        [self createscrollview];
      
//        [_themeArr release];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 
    }];
    
}
-(void)createscrollview{
    self.themeScrollView.contentSize=CGSizeMake(115*(self.themeArr.count), 0);

    themeModel  *theme =[[themeModel alloc]init];
    for (NSInteger i=0; i<self.themeArr.count; i++) {
        theme = self.themeArr[i];
        self.view=[[UIView alloc]initWithFrame:CGRectMake((i)*115, 0, 110, 150)];
        self.view.backgroundColor=[UIColor whiteColor];
        self.mybutton =[UIButton buttonWithType:UIButtonTypeCustom];
      
        self.mybutton.frame=CGRectMake(8, 8, 102, 142);
        [self.buttonarr addObject: self.mybutton];

        [self.view addSubview:self.mybutton];
        [self.themeScrollView addSubview:self.view];
        self.mybutton.layer.masksToBounds=YES;
        
        [self.mybutton sd_setBackgroundImageWithURL:[NSURL URLWithString:theme.cover_path] forState:UIControlStateNormal];
        
        [self.mybutton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

        
        
        [self.view release];
  
    }
    
    
}

-(void)layoutSubviews
{   // 左视图的位置
    self.themeScrollView.frame=CGRectMake(0, 0, self.contentView.frame.size.width, 150);

   
}


-(void)click:(UIButton *)button{
    for (NSInteger i=0; i<self.buttonarr.count; i++) {
        if (button == self.buttonarr[i]) {
            themeModel   *theme =[[themeModel alloc]init];
            theme = self.themeArr[i];
            NSLog(@"%@",theme.bid);
            [self.delegatett clicktt:button model:theme];
            
                  }
        
    }
  }


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
